package jjn.round1;

import java.util.Arrays;

/**
 * @author Jiang Jining
 * @since 2023-05-28 20:33
 */
public class Interview16_6_SmallestDifference {
    public int smallestDifference(int[] a, int[] b) {
        Arrays.sort(a);
        Arrays.sort(b);
        long minDifference = Long.MAX_VALUE;
        int first = 0, second = 0;
        while (first < a.length && second < b.length) {
            if (a[first] == b[second]) {
                return 0;
            }
            long diff = (long) a[first] - (long) b[second];
            minDifference = Math.min(Math.abs(diff), minDifference);
            if (diff > 0) {
                second++;
            } else {
                first++;
            }
        }
        return (int) minDifference;
    }
}
